# 流式API测试脚本
# 用于测试流式输出功能

Write-Host "=== Spring AI Ollama Demo 流式API 测试 ===" -ForegroundColor Green

$baseUrl = "http://localhost:8082/api/ollama"

Write-Host "`n1. 测试流式聊天接口" -ForegroundColor Yellow
Write-Host "发送请求到: $baseUrl/chat-stream" -ForegroundColor Cyan

try {
    $body = @{
        prompt = "请详细介绍一下Spring AI框架"
    } | ConvertTo-Json

    Write-Host "请求体: $body" -ForegroundColor Gray
    Write-Host "`n开始接收流式响应:" -ForegroundColor Yellow
    Write-Host "----------------------------------------" -ForegroundColor Gray

    # 使用Invoke-WebRequest来处理流式响应
    $response = Invoke-WebRequest -Uri "$baseUrl/chat-stream" -Method POST -Body $body -ContentType "application/json" -TimeoutSec 60
    
    if ($response.StatusCode -eq 200) {
        Write-Host "流式响应接收完成" -ForegroundColor Green
        Write-Host "响应长度: $($response.Content.Length) 字符" -ForegroundColor Cyan
    } else {
        Write-Host "请求失败，状态码: $($response.StatusCode)" -ForegroundColor Red
    }
} catch {
    Write-Host "流式请求失败: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host "`n----------------------------------------" -ForegroundColor Gray

Write-Host "`n2. 测试带系统提示的流式聊天接口" -ForegroundColor Yellow
Write-Host "发送请求到: $baseUrl/chat-with-system-stream" -ForegroundColor Cyan

try {
    $body = @{
        prompt = "今天天气怎么样？"
        systemMessage = "你是一个友善的天气助手，总是提供准确的天气信息。"
    } | ConvertTo-Json

    Write-Host "请求体: $body" -ForegroundColor Gray
    Write-Host "`n开始接收流式响应:" -ForegroundColor Yellow
    Write-Host "----------------------------------------" -ForegroundColor Gray

    $response = Invoke-WebRequest -Uri "$baseUrl/chat-with-system-stream" -Method POST -Body $body -ContentType "application/json" -TimeoutSec 60
    
    if ($response.StatusCode -eq 200) {
        Write-Host "流式响应接收完成" -ForegroundColor Green
        Write-Host "响应长度: $($response.Content.Length) 字符" -ForegroundColor Cyan
    } else {
        Write-Host "请求失败，状态码: $($response.StatusCode)" -ForegroundColor Red
    }
} catch {
    Write-Host "流式请求失败: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host "`n=== 流式测试完成 ===" -ForegroundColor Green
Write-Host "`n💡 提示:" -ForegroundColor Magenta
Write-Host "- 流式响应需要支持SSE的客户端才能看到实时效果" -ForegroundColor White
Write-Host "- 建议使用浏览器访问 http://localhost:8082/stream.html 体验完整的流式效果" -ForegroundColor White
Write-Host "- 或者使用curl命令: curl -N -X POST http://localhost:8082/api/ollama/chat-stream -H 'Content-Type: application/json' -d '{\"prompt\":\"你好\"}'" -ForegroundColor White
